package com.jianghu.helpers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import android.content.Context;

import com.jianghu.R;

public class GetLineFromtxt {

	public static String readAppointedLineNumber(Context context, int lineNumber) {
		InputStream in = context.getResources().openRawResource(
				R.raw.usertrends);
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(in, "utf8"));
			LineNumberReader reader = new LineNumberReader(br);
			String line = reader.readLine();
			if (lineNumber < 0 || lineNumber > getTotalLines(br)) {
				//System.out.println("�����ļ�������Χ֮�ڡ�");
			}
			{
				int num = 0;
				while (line != null) {
					if (lineNumber == ++num) {
					//	System.out.println("line     " + lineNumber + ":     "+ line);
						return line;
					}
					line = reader.readLine();
				}
				return null;
			}

		} catch (Exception e) {
			return null;
		}

	}

	static int getTotalLines(BufferedReader sourceFile) throws IOException {
		BufferedReader in = new BufferedReader(sourceFile);
		LineNumberReader reader = new LineNumberReader(in);
		String s = reader.readLine();
		int lines = 0;
		while (s != null) {
			lines++;
			s = reader.readLine();
		}
		reader.close();
		in.close();
		return lines;
	}
}
